package com.example.coinhelper.Manage;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.coinhelper.Bean.Note;
import com.example.coinhelper.db.NoteDBHelper;

import java.util.ArrayList;
import java.util.List;

public class NoteManager {
    private NoteDBHelper dbHelper;
    private SQLiteDatabase database;

    public NoteManager(Context context) {
        dbHelper = new NoteDBHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public void open() {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public void addNote(Note note) {
        ContentValues values = new ContentValues();
        values.put(NoteDBHelper.COLUMN_USERNAME, note.getUsername());
        values.put(NoteDBHelper.COLUMN_TITLE, note.getTitle());
        values.put(NoteDBHelper.COLUMN_BODY, note.getBody());
        values.put(NoteDBHelper.COLUMN_DATE, note.getDate());

        database.insert(NoteDBHelper.TABLE_NAME, null, values);
    }

    @SuppressLint("Range")
    public List<Note> getAllNotes() {
        List<Note> noteList = new ArrayList<>();
        Cursor cursor = database.query(NoteDBHelper.TABLE_NAME, null, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setId(cursor.getLong(cursor.getColumnIndex(NoteDBHelper.COLUMN_ID)));
                note.setUsername(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_USERNAME)));
                note.setTitle(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_TITLE)));
                note.setBody(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_BODY)));
                note.setDate(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_DATE)));
                noteList.add(note);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return noteList;
    }
    @SuppressLint("Range")
    public List<Note> getAllNotesByUsername(String username) {
        List<Note> noteList = new ArrayList<>();
        String selection = NoteDBHelper.COLUMN_USERNAME + "=?";
        String[] selectionArgs = { username };

        Cursor cursor = database.query(
                NoteDBHelper.TABLE_NAME,
                null,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setId(cursor.getLong(cursor.getColumnIndex(NoteDBHelper.COLUMN_ID)));
                note.setUsername(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_USERNAME)));
                note.setTitle(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_TITLE)));
                note.setBody(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_BODY)));
                note.setDate(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_DATE)));

                noteList.add(note);
            } while (cursor.moveToNext());

            cursor.close();
        }

        return noteList;
    }
    @SuppressLint("Range")
    public Note getNoteById(long noteId) {
        Note note = null;
        String selection = NoteDBHelper.COLUMN_ID + "=?";
        String[] selectionArgs = { String.valueOf(noteId) };

        Cursor cursor = database.query(
                NoteDBHelper.TABLE_NAME,
                null,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            note = new Note();
            note.setId(cursor.getLong(cursor.getColumnIndex(NoteDBHelper.COLUMN_ID)));
            note.setUsername(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_USERNAME)));
            note.setTitle(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_TITLE)));
            note.setBody(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_BODY)));
            note.setDate(cursor.getString(cursor.getColumnIndex(NoteDBHelper.COLUMN_DATE)));
            cursor.close();
        }

        return note;
    }
    public void updateNoteById(Note note) {
        ContentValues values = new ContentValues();
        values.put(NoteDBHelper.COLUMN_TITLE, note.getTitle());
        values.put(NoteDBHelper.COLUMN_BODY, note.getBody());
        values.put(NoteDBHelper.COLUMN_DATE, note.getDate());

        String selection = NoteDBHelper.COLUMN_ID + "=?";
        String[] selectionArgs = { String.valueOf(note.getId()) };

        database.update(NoteDBHelper.TABLE_NAME, values, selection, selectionArgs);
    }
    public void deleteById(Note note) {
        String selection = NoteDBHelper.COLUMN_ID + "=?";
        String[] selectionArgs = { String.valueOf(note.getId()) };

        database.delete(NoteDBHelper.TABLE_NAME, selection, selectionArgs);
    }



    // You can add more methods here like update, delete based on your requirements.
}
